package com.example.treehole.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.treehole.App;
import com.example.treehole.PostDetailActivity;
import com.example.treehole.SendPostActivity;
import com.example.treehole.base.BaseBindingFragment;
import com.example.treehole.base.BindAdapter;
import com.example.treehole.bean.Post;
import com.example.treehole.databinding.FragmentPostBinding;
import com.example.treehole.databinding.ItemPostBinding;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PostFragment extends BaseBindingFragment<FragmentPostBinding> {

    private void initBanner() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("http://192.168.43.179:8911/images/banner_image1.jpg");
        strings.add("http://192.168.43.179:8911/images/banner_image2.jpg");
        strings.add("http://192.168.43.179:8911/images/banner_image3.png");
        viewBinder.banner.setAdapter(new BannerImageAdapter<String>(strings) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                loadImage(data, holder.imageView);
            }
        }).setIndicator(new CircleIndicator(requireContext()));
    }

    private BindAdapter<ItemPostBinding, Post> postAdapter = new BindAdapter<ItemPostBinding, Post>() {
        @Override
        public ItemPostBinding createHolder(ViewGroup parent) {
            return ItemPostBinding.inflate(getLayoutInflater(), parent, false);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void bind(ItemPostBinding itemPostBinding, Post post, int position) {
            itemPostBinding.getRoot().setOnClickListener(view -> {
                startActivity(PostDetailActivity.class, intent -> {
                    intent.putExtra("post", post);
                });
            });

            itemPostBinding.tvUpTime.setText(post.time);

            itemPostBinding.title.setText(post.title);
            loadImage(post.face, itemPostBinding.ivFace);
            itemPostBinding.tvUpName.setText(post.nickname);
            if (!post.file.isEmpty()) {
                itemPostBinding.ivImage.setVisibility(View.VISIBLE);
                loadImage(post.file, itemPostBinding.ivImage);
            } else {
                itemPostBinding.ivImage.setVisibility(View.GONE);
            }

        }
    };
    private String type = "全部";

    private void doDelete(Post post) {
        showLoading();
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", post.id);
        HttpUtil.delete(Api.DELETE_POST, params, new HttpUtil.OnNetStringListener() {
            @Override
            public void success(String data, String message) {
                postAdapter.getData().remove(post);
                postAdapter.notifyDataSetChanged();
                display$Toast(message);



            }

            @Override
            public void error(String message) {
                display$Toast(message);
            }
        });
    }

    @Override
    protected void initListener() {

        viewBinder.backBtn.setVisibility(View.GONE);
        viewBinder.tvTitle.setText("首页");
        viewBinder.tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(SendPostActivity.class);
            }
        });
        viewBinder.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestData();
            }
        });
        viewBinder.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                type = tab.getText().toString();
                requestData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        initBanner();
    }

    @Override
    protected void initData() {
        viewBinder.rvTips.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        viewBinder.rvTips.setAdapter(postAdapter);
        requestData();
        viewBinder.tvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewBinder.tvHot.setTextColor(Color.parseColor("#cccccc"));
                viewBinder.tvTime.setTextColor(Color.parseColor("#3AD0F9"));
                hot = "时间";
                requestData();
            }
        });
        viewBinder.tvHot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewBinder.tvTime.setTextColor(Color.parseColor("#cccccc"));
                viewBinder.tvHot.setTextColor(Color.parseColor("#3AD0F9"));
                hot = "热度";
                requestData();
            }
        });
    }

    private String hot = "时间";

    private void requestData() {
        showLoading();
        HashMap<String, Object> params = new HashMap<>();
        params.put("key", viewBinder.etSearch.getText().toString());
        params.put("type", type);
        params.put("hot", hot);
        HttpUtil.getList(Api.POST_SEARCH, params, Post.class, new HttpUtil.OnNetListListener<Post>() {
            @Override
            public void success(List<Post> data, String message) {
                dismissLoading();
                postAdapter.getData().clear();
                postAdapter.getData().addAll(data);
                viewBinder.rvTips.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
                viewBinder.rvTips.setAdapter(postAdapter);
            }

            @Override
            public void error(String message) {
                display$Toast(message);
            }
        });
    }
}